<?php
/* 
 *销售订单
 */
class OrderAction extends TransAction {
    public function _filter(&$map) {
        //TODO
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            $where['Product.name'] = array('like',"%".$key."%");//产品名称
            $where['Order_detail.mano'] = array('like',"%".$key."%");
            $where['Sale.po'] = array('like',"%".$key."%");
            $where['Sale.remark'] = array('like',"%".$key."%");
            $where['Product.cas'] = array('like',"%".$key."%");
            $where['Product.ename'] = array('like',"%".$key."%");
            //$where['Customer.name'] = array('like',"%".$key."%");
            $where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if(!empty($_REQUEST['customer']))
            $map['_string'] = "(Customer.name like '%".$_REQUEST['customer']."%') or (Customer.company like '%".$_REQUEST['customer']."%')";

        if( $_REQUEST['dstatus'] != "")
            $map['Order_detail.status'] = $_REQUEST['dstatus'];

        if( $_REQUEST['priority'] != ""){
            if($_REQUEST['priority'] != 3 ){ //非催单
                 $map['Order_detail.priority'] = $_REQUEST['priority'];
                 $map['Order_detail.status'] = array('lt',5); //未打包
            }
            else{
                 $map['Order_detail.priority'] = array('between','4,6'); //催单
                 $map['Order_detail.status'] = array('lt',5); //未打包
            }
        
        }
        if(isset($_REQUEST['charge_man']) && $_REQUEST['charge_man'] != ''  )
            $map['Order_detail.charge_man'] = $_REQUEST['charge_man'];

        if(isset($_REQUEST['dreceipt_status']) && $_REQUEST['dreceipt_status'] != '' )
            $map['Order_detail.receipt_status'] = $_REQUEST['dreceipt_status'];

        //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );

        //选择了时间类型,并且时间不为空，则按时间进行搜索
        if( $_REQUEST['time_type'] != "" && 
          ($_REQUEST['end_time'] != '' ||  $_REQUEST['start_time'] != '' ) ){
            switch ($_REQUEST['time_type']) {
              case '1': //交货期
                $map['Order_detail.delivery_date '] = $cond_time;
                break;
              case '2': //创建时间
                $map['Order_detail.create_time '] = $cond_time;
                break;
              
              default:  //按下单时间查询
                $map['Order_detail.create_time '] = $cond_time;
                break;
            }
            
        }
    }

    public function  index() {
        parent::index('OrderView');
    }
    public function full_edit($value='')
    {
        $this->edit();
    }
     //相关产品
    public function detail($value='')
    {

        $map = 'order_id = '.intval($_REQUEST['id']) ;
        $count = D('OrderView')->where($map)->count();
        if($count > 0){
            import ( "@.ORG.Page" );
            $page = new Page($count,25);
            $ps = D('OrderView')->where($map)->limit($page->firstRow.','.$page->listRows)->order('did desc')->findAll(); 
            $assign_data = array(
                'products' => $ps,
                'page' => $page->show(),
            ); 
            $this->assign($assign_data);
        }
        $this->display();
    }

    public function _before_insert(){
        $_POST['real_pay'] = $_POST['prepay']+$_POST['remain_money'];
    }

    public function _before_update(){
        $_POST['real_pay'] = $_POST['prepay']+$_POST['remain_money'];
    }

    public function check(){
         $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $condition['status'] = 1; //待审批
             $order = M('Order');
             $list = $order->where($condition)->findAll();
             $i = 0;
             foreach ($list as $key => $value) {
                 $order->where('id = '.$value['id'])->setField('status',2);
                $this->massChangeStatus($value['id'] , 0);   //小订单为“待确认”
                $i++;
             }
             $this->success($i." 个订单审批通过！");
        }else{
            $this->error("请选择待审批的订单！");
        }
    }

    public function checkFail(){
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $condition['status'] = 1; //待审批
             $order = M('Order');
             $list = $order->where($condition)->findAll();
             $i = 0;
             foreach ($list as $key => $value) {
                 $order->where('id = '.$value['id'])->setField('status',-2);
                $this->massChangeStatus($value['id'] , -2);   //小订单为“待确认”
                $i++;
             }
             $this->success($i." 个订单审批不通过！！");
        }else{
            $this->error("请选择待审批的订单！");
        }
    }

    //批量修改大订单下的小订单状态
    public function massChangeStatus($order_id,$status){
        M('Order_detail')->where('order_id = '.$order_id)->setField('status',$status);
    }

    public function get_customer_address()
    {
        $data = array();
        $customer_id = $_REQUEST['customer_id'];
        $address = M('Customer')->where('id = '.$customer_id)->getField('address');
        if($address){
            $data['address'] = $address;
            $this->ajaxReturn($data, '', 1);
        }   
        else
            $this->ajaxReturn(null, '无法获取客户地址！', 0);
    }

     //分配责任人
    public function charge()
    {
        $id = $_REQUEST['id'];
        if (isset ( $id )) {
          
            $condition = array ('order_id' => array ('in', explode ( ',', $id ) ) );
            $list = M('Order_detail') ->where ( $condition )->findAll();
            $sale_ids = array(); //有效的SALE_ID
            if(!is_array($list))
                $this->error('您选择的销售单不存在！');
            $this->assign('receives',$list);
            $this->display();
        }else
          $this->error("请选择销售单！");
    }

    public function print_check_order(){
         $vo = M('Order')->getById($_REQUEST['id']);
         $sum_info = M('Order_detail')->where('order_id = '.$_REQUEST['id'])->field("sum(logifee) as slogifee,sum(total) as stotal")->find();
         
         $vo['total'] = $sum_info['stotal']+ $sum_info['slogifee']; // 总金额 = 总货款+总运费 
         $this->assign('vo', $vo );
         $this->display();        
    } 

}
?>
